<template xmlns="http://www.w3.org/1999/html">
    <sea-data-table ref="tableObject" url="/sys/excel/content"
                    :auto-load="false" :where="where" :result-parser="resultParser">
      <!--表头-->
      <sea-table-header slot="header" disabled-form>
        <!--标题栏-->
        <sea-table-title slot="title" title="Excel内容展示" subtitle="500KB以上的文件，仅展示前100条数据，可以点击右侧下载按钮下载原件！">
        </sea-table-title>

        <template slot="buttons">
          <el-button icon="fa fa-download" @click="download" title="下载Excel原件" plain size="small">下载</el-button>
        </template>
      </sea-table-header>

      <!--表头-->
      <template slot="columns">
        <el-table-column
            v-for="(item, idx) in headers" :key="idx"
            :property="item.name" :label="item.label"></el-table-column>
      </template>
    </sea-data-table>
</template>

<script>

export default {
    components: {}
    , props: {
        // 成功回调，通常是刷新数据表格
        onSucceed: {type: Function, default: undefined}
    }
    , data() {
        return {
            // 操作名称
            operation: undefined
            // 行级数据
            , row_data: undefined
            // 查询条件
            , where: undefined
            // 表头
            , headers: []
        }
    }
    , methods: {
        /**
         * 处理解雇
         */
        resultParser: function (ret) {
            if (ret.code === 0) {
                let data = ret.data;
                let header = [];
                let names = data.names;
                let labels = data.labels;
                for (let i = 0; i < names.length; i++) {
                    header.push({name: names[i], label: labels[i]});
                }
                this.headers = header;
                return data.content;
            } else {
                return {total: 0, list: []}
            }
        },
        /**
         * 新增/编辑操作成功，关闭当前窗口
         *
         * @param ret 后台响应数据
         */
        callback: function (ret) {
            this.$layers.succeed('操作成功!');
            // 刷新表单列表
            this.onSucceed(ret);
            // 关闭弹窗
            this.$refs.dialog.close();
        },
        /**
         * 操作栏事件分发
         * @param evt 事件
         * @param row 行级对象
         * @param old 可供回滚的参数
         */
        onAction: function (evt, row, old) {
            this.operation = evt;
            if (evt === 'info' || evt === '$excelName') {
                let dialog = {title: row.excelName, fullscreen: true};
                this.$refs.dialog.open(dialog).then(() => {
                    this.row_data = row;
                    this.where = {id: row.id};
                });
            } else if (evt === 'download') {
                this.$layers.downloadFile('/sys/excel/download/' + row.id)
            } else if (evt === 'remove') {
            }
        },
        /**
         * 下载文件
         */
        download: function () {
            window.open(this.$layers.getContextPath() + '/sys/excel/download?id=' + this.row_data.id);
        }
    }
    //end
}
</script>
